local app = require 'app'
local game = app.game.running

local phase_shower = game.ui.element()

phase_shower.store_watchers = 
{
    ingame = 
    {
        ['$'] = function() end,

        ['阶段变更'] = function(self, store, state)
            self:on_pahse(state.phase_id)
        end
    }
}

function phase_shower:ctor(ingame)
    self.res = 'club.threecard.doudizhu/ui/ingame.b:phase_shower'
    self.ingame = ingame
end

--空闲中...
local on_idle = function(self)
    for i = 1, self.ingame.seats.max do
        local seat = self.ingame.seats[i]
        if seat and seat.player then
            seat:ready_change(seat.player.is_ready == 1)
            seat:mark_pass(false)
            seat:mark_lord(false)
            seat.bid_result:hide()
        end
    end

    if game.kbe.player().is_ready == 1 then
        self.ingame.ready:hide()
    else
        self.ingame.ready:show()
    end
end

--游戏中...
local on_gaming = function(self)
    for i = 1, self.ingame.seats.max do
        local seat = self.ingame.seats[i]
        if seat and seat.player then
            seat:ready_change(false)
        end
    end
    self.ingame.ready:hide()
end

local clear_bid = function(self)
    for i = 1, self.ingame.seats.max do
        local seat = self.ingame.seats[i]
        seat.bid_result:hide()
    end
    self.ingame.bidder:hide()
end

function phase_shower:on_pahse(phase_id)
    if phase_id == game.util.consts.phase_idle.id
    or phase_id == game.util.consts.phase_nil.id then
        on_idle(self)
    elseif phase_id == game.util.consts.phase_start.id then
        on_gaming(self)
        clear_bid(self)
    elseif phase_id == game.util.consts.phase_bid.id then
        on_gaming(self)
        clear_bid(self)
    elseif phase_id == game.util.consts.phase_handout.id then
        on_gaming(self)
        clear_bid(self)
    elseif phase_id == game.util.consts.phase_over.id then
        on_gaming(self)
        clear_bid(self)
    end
end

function phase_shower:clear_scene()
    self:hide()
end

return phase_shower